package com.example.pda.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Toast;

import com.example.pda.R;
import com.example.pda.common.BaseActivity;
import com.example.pda.common.SharedUtils;
import com.example.pda.common.Utils;
import com.example.pda.databinding.ActivityLogin2Binding;
import com.example.pda.databinding.ActivityLoginBinding;
import com.example.pda.net.HttpManager;
import com.example.pda.ui.main.MainActivity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

public class LoginActivity extends BaseActivity {

    private LoginViewModel loginViewModel;
    private ActivityLogin2Binding binding;

    private String mBaseUrl;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login2);
        binding.setLifecycleOwner(this);
        loginViewModel = new ViewModelProvider(this, new LoginViewModelFactory())
                .get(LoginViewModel.class);
        registerObserve(loginViewModel);

        binding.abc.getTitleTextView().setText("登陆");
        binding.login.setText("登陆");

        binding.username.setText(SharedUtils.getUserName());
        binding.username.setHint("请输入用户名");
        binding.password.setText("");
        binding.password.setHint("请输入密码");
        binding.login.setEnabled(false);

        loginViewModel.getLoginFormState().observe(this, new Observer<LoginFormState>() {
            @Override
            public void onChanged(@Nullable LoginFormState loginFormState) {
                if (loginFormState == null) {
                    return;
                }
                binding.login.setEnabled(loginFormState.isDataValid());
                if (loginFormState.getUsernameError() != null) {
                    binding.username.setError(getString(loginFormState.getUsernameError()));
                }
                if (loginFormState.getPasswordError() != null) {
                    binding.password.setError(getString(loginFormState.getPasswordError()));
                }
            }
        });
        loginViewModel.getLoginResult2().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean loginResult) {
                if (loginResult != null && loginResult) {
                    updateUiWithUser();
                }
            }
        });


        TextWatcher afterTextChangedListener = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // ignore
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // ignore
            }

            @Override
            public void afterTextChanged(Editable s) {
                loginViewModel.loginDataChanged(binding.username.getText().toString(),
                        binding.password.getText().toString());
            }
        };
        binding.username.addTextChangedListener(afterTextChangedListener);
        binding.password.addTextChangedListener(afterTextChangedListener);

        binding.login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String http = binding.username.getText().toString().trim();
                String httpPort = binding.password.getText().toString().trim();
                loginViewModel.login(http, httpPort);
            }
        });
    }

    private void updateUiWithUser() {
        Toast.makeText(getApplicationContext(), "登陆成功", Toast.LENGTH_LONG).show();
        finish();
    }
}